/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/7/17 22:14
 */
// 多数元素
    // https://leetcode.cn/problems/majority-element/description/
    //关键在于要求双复杂度在O(N),次数大于n/2
public class Solution169 {
    public int majorityElement(int[] nums) {
        int target = 0;
        // 表示候选者为多数元素的可能性大小
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (count == 0) {
                target = nums[i];
                count = 1;
            } else if (nums[i] == target) {
                count++;
            } else {
                count--;
            }

        }
        return target;
    }
}
